#!/bin/bash

# MongoDB初始化集合脚本
# RedFire Trading System - MongoDB Collections Setup Script

set -e

echo "=================================================="
echo "RedFire MongoDB集合初始化开始..."
echo "Time: $(date)"
echo "=================================================="

# 等待MongoDB启动并认证
echo "连接到MongoDB并创建集合..."

mongosh --host localhost:27017 -u redfire_app -p redfire_app_mongo_2025 --authenticationDatabase redfire_docs <<EOF

use redfire_docs

// 创建交易记录集合
db.createCollection("trading_records", {
  validator: {
    \$jsonSchema: {
      bsonType: "object",
      required: ["symbol", "timestamp", "action", "quantity", "price"],
      properties: {
        symbol: {
          bsonType: "string",
          description: "交易品种代码"
        },
        timestamp: {
          bsonType: "date", 
          description: "交易时间戳"
        },
        action: {
          bsonType: "string",
          enum: ["buy", "sell"],
          description: "交易动作"
        },
        quantity: {
          bsonType: "number",
          minimum: 0,
          description: "交易数量"
        },
        price: {
          bsonType: "number",
          minimum: 0,
          description: "交易价格"
        },
        order_id: {
          bsonType: "string",
          description: "订单ID"
        },
        strategy_id: {
          bsonType: "string", 
          description: "策略ID"
        },
        commission: {
          bsonType: "number",
          description: "手续费"
        },
        profit_loss: {
          bsonType: "number",
          description: "盈亏"
        }
      }
    }
  }
});

// 创建策略配置集合
db.createCollection("strategy_configs", {
  validator: {
    \$jsonSchema: {
      bsonType: "object",
      required: ["strategy_id", "strategy_name", "created_at"],
      properties: {
        strategy_id: {
          bsonType: "string",
          description: "策略唯一标识"
        },
        strategy_name: {
          bsonType: "string", 
          description: "策略名称"
        },
        description: {
          bsonType: "string",
          description: "策略描述"
        },
        parameters: {
          bsonType: "object",
          description: "策略参数"
        },
        created_at: {
          bsonType: "date",
          description: "创建时间"
        },
        updated_at: {
          bsonType: "date",
          description: "更新时间"
        },
        status: {
          bsonType: "string",
          enum: ["active", "inactive", "testing"],
          description: "策略状态"
        }
      }
    }
  }
});

// 创建市场数据集合
db.createCollection("market_data", {
  validator: {
    \$jsonSchema: {
      bsonType: "object",
      required: ["symbol", "timestamp", "price"],
      properties: {
        symbol: {
          bsonType: "string",
          description: "品种代码"
        },
        timestamp: {
          bsonType: "date",
          description: "数据时间戳"
        },
        price: {
          bsonType: "number",
          description: "价格"
        },
        volume: {
          bsonType: "number",
          description: "成交量"
        },
        bid: {
          bsonType: "number",
          description: "买一价"
        },
        ask: {
          bsonType: "number", 
          description: "卖一价"
        },
        open: {
          bsonType: "number",
          description: "开盘价"
        },
        high: {
          bsonType: "number",
          description: "最高价"
        },
        low: {
          bsonType: "number",
          description: "最低价"
        },
        close: {
          bsonType: "number",
          description: "收盘价"
        }
      }
    }
  }
});

// 创建用户配置集合
db.createCollection("user_configs");

// 创建日志集合
db.createCollection("system_logs");

// 创建索引
print("创建索引...");

// 交易记录索引
db.trading_records.createIndex({ "symbol": 1, "timestamp": -1 });
db.trading_records.createIndex({ "order_id": 1 }, { unique: true });
db.trading_records.createIndex({ "strategy_id": 1, "timestamp": -1 });
db.trading_records.createIndex({ "timestamp": -1 });

// 策略配置索引
db.strategy_configs.createIndex({ "strategy_id": 1 }, { unique: true });
db.strategy_configs.createIndex({ "strategy_name": 1 });
db.strategy_configs.createIndex({ "status": 1 });

// 市场数据索引
db.market_data.createIndex({ "symbol": 1, "timestamp": -1 });
db.market_data.createIndex({ "timestamp": -1 });

// 系统日志索引
db.system_logs.createIndex({ "timestamp": -1 });
db.system_logs.createIndex({ "level": 1, "timestamp": -1 });

print("集合和索引创建完成!");

EOF

echo "=================================================="
echo "MongoDB集合初始化完成!"
echo "创建的集合:"
echo "- trading_records (交易记录)"
echo "- strategy_configs (策略配置)"
echo "- market_data (市场数据)"
echo "- user_configs (用户配置)"
echo "- system_logs (系统日志)"
echo "=================================================="
